package com.ztom.top100;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 把二叉搜索树转换为累加树
 * <p>
 * https://leetcode-cn.com/problems/convert-bst-to-greater-tree/
 *
 * @author ZhangTao
 */
public class Code93ConvertBST {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    /**
     * 倒序中序遍历, 递归
     *
     * @param root
     * @return
     */
//    int sum = 0;
//    public TreeNode convertBST2(TreeNode root) {
//        if (root == null) {
//            return null;
//        }
//        convertBST2(root.right);
//        sum += root.val;
//        root.val = sum;
//        convertBST2(root.left);
//
//        return root;
//    }
    public TreeNode convertBST2(TreeNode root) {
        if (root == null) {
            return null;
        }
        process(root, 0);
        return root;
    }

    private int process(TreeNode node, int parentVal) {
        if (node == null) {
            return parentVal;
        }
        node.val += process(node.right, parentVal);
        return process(node.left, node.val);
    }

    /**
     * 倒序中序遍历, 迭代
     *
     * @param root
     * @return
     */
    public TreeNode convertBST1(TreeNode root) {
        if (root == null) {
            return null;
        }

        int sum = 0;
        Deque<TreeNode> stack = new LinkedList<>();
        TreeNode node = root;
        while (!stack.isEmpty() || node != null) {
            if (node != null) {
                stack.addLast(node);
                node = node.right;
            } else {
                node = stack.pollLast();
                sum += node.val;
                node.val = sum;

                node = node.left;
            }
        }

        return root;
    }

    /**
     * 改写 morris 遍历
     *
     * @param root
     * @return
     */
    public TreeNode convertBST(TreeNode root) {
        if (root == null) {
            return null;
        }

        TreeNode cur = root;
        TreeNode mostLeft = null;
        int sum = 0;
        while (cur != null) {
            mostLeft = cur.right;
            if (mostLeft != null) {
                while (mostLeft.left != null && mostLeft.left != cur) {
                    mostLeft = mostLeft.left;
                }

                if (mostLeft.left == null) {
                    // 第一次到达
                    mostLeft.left = cur;
                    // 继续去右树
                    cur = cur.right;
                } else {
                    // 第二次到达
                    mostLeft.left = null;
                    sum += cur.val;
                    cur.val = sum;
                    cur = cur.left;
                }
            } else {
                sum += cur.val;
                cur.val = sum;
                cur = cur.left;
            }
        }

        return root;
    }
}
